<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once 'model/db.php';
?>
<link rel="stylesheet" href="public/css/style_ontap.css" type="text/css">

<?php
$query = "SELECT * FROM companies";
$companies = getFilms($query);
?>
<div id="form">
    <h1>vantan</h1>
    <form method="POST">
        Min fans<input type="text" name="min_fans"><br>
        Max fans<input type="text" name="max_fans"><br>
        Keyword <input type="text" name="keyword"><br>
        Company<select name="id_company">
            <?php foreach ($companies as $company): ?>
                <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
            <?php endforeach; ?>
        </select>
        <input type="submit" name="submit">
    </form>
</div>
<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans']; //TODO
    $max_fans = $_POST['max_fans']; //TODO
    $keyword = $_POST['keyword']; //TODO
    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages 
                  
                  WHERE
                      (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                      
                  LIMIT 0, 10
                  ";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
             WHERE (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    } else {
        $query = "SELECT * FROM pages    
                  WHERE
                      (likes >= $min_fans)
                      AND (likes <= $max_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";
    }

    $pages = getFilms($query);
    ?>
<?php else : ?> 
        <?php
        $query = "SELECT * FROM pages LIMIT 0,10";
        $pages = getFilms($query);
        ?>
    <?php endif; ?>
    <?php if (!empty($pages)): ?>
    
    <table id="bang">
        <tr class="dong1">
            <td> profile </td>
            <td> likes </td>
            <td> description </td>
        </tr>
        <?php foreach ($pages as $page): ?>
            <tr class="dong2">
                <td><img src="<?php echo $page['picture'] ?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td> <?php echo substr($page['description'], 0, 100); ?></td>
            </tr>
        <?php endforeach; ?>
    </table> 
<?php endif; ?>


